$(document).ready(function() {
    $.getJSON('controller/cont.ruh.php', {aksi_get_user_ubah_pass: true}, function(data) {
        document.getElementById('nip').value = data.nip;
        document.getElementById('id_user').value = data.id_user;
    });

    $('#btn_ubah_pass').click(function(e) {
        e.preventDefault();
        var id_user = $('#id_user').val();
        var password_lama = $('#password_lama').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();

        if (password_lama === '' || password === '' || password2 === '') {
            $('#error').html('Semua Harus Diisi').fadeIn(500).delay(2500).fadeOut(500);
            clear_input();
        } else if (password !== password2) {
            $('#error').html('Konfirmasi Password Salah').fadeIn(500).delay(2500).fadeOut(500);
            clear_input();
        } else if (password.length < 6) {
            $('#error').html("Password Minimal 6 karakter").fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.ruh.php', {aksi_ubah_password: true, id_user: id_user,
                password_lama: password_lama, password: password}, function(data) {
                if (data.msg) {
                    $('#success').html('Password Berhasil Diubah, Silahkan Logout dan Login Lagi').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input();
                } else {
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                    clear_input();
                }
            }, 'json');
        }


        return false;
    });

    return false;
});

function clear_input() {
    document.getElementById('password_lama').value = "";
    document.getElementById('password').value = "";
    document.getElementById('password2').value = "";
}